package com.func;

import java.util.function.Function;

/**
 * https://zhuanlan.zhihu.com/p/357918015
 * 基于消费函数生成新函数
 *
 * @author: chenhs
 * @date: Created in 10:31 2022/7/12
 **/
public class TransformFunction {

    static Function<I, O> transform(Function<I, O> in) {

        //普通方法
        /*return in.andThen(new Function<O, O>() {
            @Override
            public O apply(O o) {
                System.out.println(o);
                return o;
            }
        });*/

        //Lambda表达式写法
        return in.andThen(o -> {
            System.out.println(o);
            return o;
        });

    }

    public static void main(String[] args) {
        //普通写法
        Function<I, O> f = transform(new Function<I, O>() {
            @Override
            public O apply(I i) {
                System.out.println(i);
                return new O();
            }
        });
        O o = f.apply(new I());

        //Lambda表达式
        Function<I, O> f1 = transform(i -> {
            System.out.println(i);
            return new O();
        });

        O o1 = f1.apply(new I());
    }


}
